//
//  PaiHangbangViewController.m
//  A_HandHeldLOL
//
//  Created by Zhao YanBin on 16/1/22.
//  Copyright © 2016年 zhaoyanbin. All rights reserved.
//

#import "PaiHangbangViewController.h"
#import "AFNModel.h"
#import "PaihangTableViewCell.h"
#import "PaihangModel.h"
#import <UIImageView+WebCache.h>
#import "PaihangCollectionViewCell.h"
@interface PaiHangbangViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic, retain)UIView *toubu;
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, retain)UILabel *paihang;
@property(nonatomic, retain)UILabel *zhaohuan;
@property(nonatomic, retain)UILabel *duanwei;

@property(nonatomic, retain)UITableView *tableView;

@property(nonatomic, retain)UIButton *button;

@property(nonatomic, retain)UICollectionView *collection;
@property(nonatomic, retain)UIView *header;

@property(nonatomic, retain)NSArray *arr;

@property(nonatomic, assign)BOOL isPlay;
@property(nonatomic, retain)NSArray *daqupaihang;
@end

@implementation PaiHangbangViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"大神排行榜";
//    
//    _dataArr = [NSMutableArray arrayWithArray:@[@"艾欧尼亚",@"比尔吉沃特"]];
//    
//    DropDownListView *dropDVC = [[DropDownListView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40) dataSource:self delegate:self];
    
//    [self.view addSubview:dropDVC];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    [self creatableView];
    
   
    
    [self data];
    
//    [self creatcollection];
    
    [self daqushuju];
}
-(void)daqushuju
{
    self.arr = @[@"艾欧尼亚",@"祖安",@"诺克萨斯",@"班德尔城",@"皮尔特沃夫",@"战争学院",@"巨神峰",@"雷瑟守备",@"裁决之地",@"黑色玫瑰",@"暗影岛",@"钢铁烈阳",@"运恒教派",@"水晶之痕",@"影流",@"守望之海",@"征服之海",@"卡拉曼达",@"皮城警备",@"比尔吉沃特",@"德玛西亚",@"弗雷尔卓德",@"无畏先锋",@"恕瑞玛",@"扭曲丛林",@"巨龙之巢",@"教育网专区"];
    self.daqupaihang = @[@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=电信一&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1452928044&p_=26477&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E4%BA%8C&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453535934&p_=20147&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E5%9B%9B&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453535999&p_=20342&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E5%9B%9B&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537351&p_=24398&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E4%BA%94&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537391&p_=24518&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E5%85%AD&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537406&p_=24563&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E4%B8%83&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537422&p_=24611&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E5%85%AB&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537438&p_=24659&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E4%B9%9D&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537455&p_=24710&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E5%8D%81&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537471&p_=24758&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E5%8D%81%E4%B8%80&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537488&p_=24809&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E5%8D%81%E4%BA%8C&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537513&p_=24884&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E5%8D%81%E4%B8%89&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537529&p_=24932&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E5%8D%81%E5%9B%9B&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537543&p_=24974&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E5%8D%81%E4%BA%94&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537559&p_=25022&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E5%8D%81%E5%85%AD&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537582&p_=25091&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E5%8D%81%E4%B8%83&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537596&p_=25133&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E5%8D%81%E5%85%AB&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537611&p_=25178&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E5%8D%81%E4%B9%9D&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537626&p_=25223&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%BD%91%E9%80%9A%E4%B8%80&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537647&p_=25286&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%BD%91%E9%80%9A%E4%BA%8C&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537661&p_=25328&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%BD%91%E9%80%9A%E4%B8%89&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537678&p_=25379&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%BD%91%E9%80%9A%E5%9B%9B&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537696&p_=25433&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%BD%91%E9%80%9A%E4%BA%94&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537714&p_=25487&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%BD%91%E9%80%9A%E5%85%AD&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537726&p_=25523&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%BD%91%E9%80%9A%E4%B8%83&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537738&p_=25559&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",@"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E6%95%99%E8%82%B2%E4%B8%80&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453537752&p_=25601&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol"];
}

-(void)creatcollection
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //cell的内容大小
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 40 )/ 3 , 30 );
    
    //上左下右
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //cell的间距
    layout.minimumLineSpacing = 10;
    
    _collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 104, self.view.frame.size.width, 400) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    
    
    [self.view addSubview:_collection];
    
    //注册重用池
    [_collection registerClass:[PaihangCollectionViewCell class] forCellWithReuseIdentifier:@"daqu"];
    [layout release];
    [_collection release];
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PaihangCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"daqu" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor yellowColor];
        cell.img.text = _arr[indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    if (_arr[0]) {
//        NSString *str = @"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=电信一&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1452928044&p_=26477&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol";
        [AFNModel GETWithURL:_daqupaihang[indexPath.item] completion:^(id result) {
            NSArray *arr = result[@"data"];
            self.dataArr = [PaihangModel modelHanderWithArray:arr];
            [_tableView reloadData];
        }];
    [_button setTitle:_arr[indexPath.row] forState:UIControlStateNormal];
    _collection.alpha = 0;
    //    [_collection removeFromSuperview];
//    }else if (_arr[1]){
//        [self data1];
//    }
}


-(void)viewVC
{
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    _button.frame = CGRectMake(0, 64, self.view.frame.size.width, 40);
    [_button setTitle:@"艾欧尼亚" forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(dianji:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_button];
    
    
    self.paihang = [[UILabel alloc] initWithFrame:CGRectMake(10,0, 40, 40)];
    _paihang.backgroundColor = [UIColor yellowColor];
    _paihang.text = @"排行";
    [_header addSubview:_paihang];
    
    self.zhaohuan = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 70, 40)];
    _zhaohuan.backgroundColor = [UIColor whiteColor];
    _zhaohuan.text = @"召唤师";
    [_header addSubview:_zhaohuan];
    
    self.duanwei = [[UILabel alloc] initWithFrame:CGRectMake(300, 0, 75, 40)];
    _duanwei.backgroundColor = [UIColor whiteColor];
    _duanwei.text = @"段位/胜点";
    [_header addSubview:_duanwei];
    
}

//button点击
-(void)dianji:(UIButton *)btn
{
    
    
    if (!_isPlay) {
        [self creatcollection];
    }else{
        if (_collection) {
            _collection.alpha = 1;
        }else{
//            [self creatcollection];
        }
        //从父视图中移除
        [_collection removeFromSuperview];
    }
    _isPlay = !_isPlay;
}

-(void)creatableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,104, self.view.frame.size.width, self.view.frame.size.height  - 64 - 49 - 40 ) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[PaihangTableViewCell class] forCellReuseIdentifier:@"paihang"];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    
    self.header = [[UIView alloc] initWithFrame:CGRectMake(0, -64, self.view.frame.size.width, 40)];
    _header.backgroundColor = [UIColor blackColor];
    [self.tableView addSubview:_header];
//    _tableView.tableHeaderView = _header;
    [_header release];
    
     [self viewVC];

}

-(void)data
{
    NSString *str = @"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=电信一&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1452928044&p_=26477&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol";
    [AFNModel GETWithURL:str completion:^(id result) {
        NSArray *arr = result[@"data"];
        self.dataArr = [PaihangModel modelHanderWithArray:arr];
        [_tableView reloadData];
 }];
}
-(void)data1
{
    NSString *str = @"http://lol.zhangyoubao.com/apis/rest/RolesService/playerRank?condition=0&server=%E7%94%B5%E4%BF%A1%E4%BA%8C&i_=583B56AA-D4B2-4B1E-B41B-D8388608FBE3&t_=1453535934&p_=20147&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol";
    [AFNModel GETWithURL:str completion:^(id result) {
        NSArray *arr = result[@"data"];
        self.dataArr = [PaihangModel modelHanderWithArray:arr];
        [_tableView reloadData];
    }];

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PaihangTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"paihang" forIndexPath:indexPath];
    PaihangModel *m = _dataArr[indexPath.row];
    cell.shu.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    cell.name.text = m.name;
    cell.duanwei.text = m.position;
    cell.shengdian.text = m.point;
    [cell.touxiang sd_setImageWithURL:[NSURL URLWithString:m.url]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}



//-(void) chooseAtSection:(NSInteger)section index:(NSInteger)index
//{
//    
//}
//
//#pragma mark -- dropdownList DataSource
//-(NSInteger)numberOfSections
//{
//    return [_dataArr count];
//}
//-(NSInteger)numberOfRowsInSection:(NSInteger)section
//{
//    NSArray *arry =_dataArr[section];
//    return [arry count];
//}
//-(NSString *)titleInSection:(NSInteger)section index:(NSInteger) index
//{
//    return _dataArr[section][index];
//}
//-(NSInteger)defaultShowSection:(NSInteger)section
//{
//    return 0;
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
